package d2_com.itheima.parameter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static java.util.Collections.sort;

public class Room {
    private List<Card> cards = new ArrayList<>();

    public Room() {
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        String[] colors = {"♥️", "♠️", "♦️", "♣️"};
        int size = 0;
        for (String number : numbers) {
            size++;
            for (String color : colors) {
                //得到一张牌
                Card c = new Card(number, color, size);
                cards.add(c);
            }
        }
        //单独存入小大王
        Card c1 = new Card("", "\uD83E\uDD21", ++size);
        Card c2 = new Card("", "\uD83C\uDCCF", ++size);
        Collections.addAll(cards, c1, c2);
    }

    public void start() {
        //1.洗牌

        Collections.shuffle(cards);

        //2.准备3个玩家
        List<Card> user1 = new ArrayList<>();
        List<Card> user2 = new ArrayList<>();
        List<Card> user3 = new ArrayList<>();

        //3.开始发牌
        for (int i = 0; i < cards.size() - 3; i++) {
            Card c = cards.get(i);
            if (i % 3 == 0) {
                user1.add(c);
            } else if (i % 3 == 1) {
                user2.add(c);
            } else if (i % 3 == 2) {
                user3.add(c);
            }
        }

        //4.底牌(三张地主牌)
        List<Card> lastThreeCards = cards.subList(cards.size() - 3, cards.size());

        //5.排序
        sort(user1);
        sort(user2);
        sort(user3);

        //6.抢地主
        user1.addAll(lastThreeCards);
        sort(user1);

        //7.看牌
        System.out.println("地主牌："+lastThreeCards);
        System.out.println("玩家1：" + user1+"（地主）");
        System.out.println("玩家2：" + user2);
        System.out.println("玩家3：" + user3);
    }

    private void sort(List<Card> c) {
       Collections.sort(c, new Comparator<Card>() {
           @Override
           public int compare(Card o1, Card o2) {
               return o2.getSize()-o1.getSize();
           }
       });
    }
}
